//==============================================================================
// REGRESSION FOR TABLE 6
//==============================================================================

clear all

//------------------------------------------------------------------------------
// MATCHED SIGNALS:  DV = attempt bottom door  
//------------------------------------------------------------------------------

* Load data 
  import delimited "./data/matched-signals.csv", encoding(ISO-8859-1) clear
  drop if ~ matched
  drop if moves > 250  // Only the first half of the game
  
* Prep vars
  gen losses  = treated
  label var losses  "Losses"
  label var signal  "Signal"
  label var moves   "Moves"
  
* LOGIT odds ratios for first half of game

  logistic dv losses , or cluster(eid) 
  outreg2 using  "./results/08_matched-signals.xls", addstat("Log likelihood",  `e(ll)') alpha(0.001,0.01,0.05) symbol(***, **, *) bdec(3) label excel eform
			
  logistic dv losses moves , or cluster(eid) 
  outreg2 using  "./results/08_matched-signals.xls", addstat("Log likelihood",  `e(ll)') alpha(0.001,0.01,0.05) symbol(***, **, *) bdec(3) label excel eform


//------------------------------------------------------------------------------
// MATCHED SIGNALS AND MOVES:   DV = attempt bottom door 
//------------------------------------------------------------------------------

* Load data  
  import delimited "./data/matched-signals-moves.csv", encoding(ISO-8859-1) clear
  drop if ~ matched    // This file includes unmatched obs too, so we can run a Heckman correction
  drop if moves > 250  // Only the first half of the game

  gen losses  = treated
  label var losses  "Losses"
  label var signal  "Signal"
  
 
* Estimate odds ratios  
  logistic dv losses   , or cluster(eid) 
  outreg2 using  "./results/08_matched-signals-moves.xls", addstat("Log likelihood",  `e(ll)') alpha(0.001,0.01,0.05) symbol(***, **, *) bdec(3) label excel eform

  logistic dv losses moves , or cluster(eid) 
  outreg2 using  "./results/08_matched-signals-moves.xls", addstat("Log likelihood",  `e(ll)') alpha(0.001,0.01,0.05) symbol(***, **, *) bdec(3) label excel eform


//------------------------------------------------------------------------------
// ANALYZE MATCHED SIGNALS AND MOVES, rebalance sample for engogeneity  
//------------------------------------------------------------------------------

* Estimate a LPM of liklihood of unmatch
  import delimited "./data/matched-signals-moves-match-rate.csv", encoding(ISO-8859-1) clear
  egen tmp = mean(rate), by(moves)
  gen miss = 1 - tmp
  gen moves_sq = moves * moves
  regress miss moves moves_sq
  local b0 = _b[_cons]
  local b1 = _b[moves]
  local b2 = _b[moves_sq]
  
  disp "b0 = `b0'"
  disp "b1 = `b1'"
  disp "b2 = `b2'"


* Load data  
  import delimited "./data/matched-signals-moves.csv", encoding(ISO-8859-1) clear
  drop if moves > 250  // Only the first half of the game
  drop if ~ matched    // Only use unmatched obs 
 
  gen losses  = treated
  label var losses  "Losses"
  label var signal  "Signal"
  gsort signal
  
* Randomly re-sample to balance out distribution across signals
  
  local b0 .1578036281019047
  local b1 .0031154904687683
  local b2 -3.88109075588e-06

  set seed 123456
  gen a = uniform()
  gen p = `b0' + moves * `b1' + (moves * moves * `b2')
  gen newsample = 0
  replace newsample = 1 if a < p  
  gen z = 1
  egen c = sum(z) if newsample, by(moves) 
  keep if newsample
  
* Estimate odds ratios  
  logit dv losses moves  , or cluster(eid)
  outreg2 using  "./results/08_matched-signals-moves.xls", ctitle("Rebalanced") addstat("Log likelihood",  `e(ll)') alpha(0.001,0.01,0.05) symbol(***, **, *) bdec(3) label excel eform
